﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MediaBot.Library
{
    public class MediaBotConverter
    {
        public static string ParseDateTime(int previousNumber, string dateType, string time)
        {
            DateTime resultTime = DateTime.Now.Date;

            if (!string.IsNullOrEmpty(time))
            {
                DateTime ts = DateTime.Parse(time);
                resultTime = resultTime.AddHours(ts.Hour);
                resultTime = resultTime.AddMinutes(ts.Minute);
            }

            switch (dateType.ToLower())
            {
                case "tuan":
                    resultTime = resultTime.AddDays(-7 * previousNumber);
                    break;
                case "ngay":
                    resultTime = resultTime.AddDays(-previousNumber);
                    break;
            }

            if (resultTime.Hour > DateTime.Now.Hour)
                resultTime = resultTime.AddDays(-1);

            return resultTime.ToString();
        }

        public static string ParseDateTime(string vnDateTime)
        {
            string resultString = DateTime.Now.ToString();
            if (!string.IsNullOrEmpty(vnDateTime))
            {
                vnDateTime = vnDateTime.ToLower().Trim();
                vnDateTime = vnDateTime.Replace("luc", "");
                vnDateTime = vnDateTime.Replace("thang", "");

                vnDateTime = vnDateTime.Replace("muoi mot", "11");
                vnDateTime = vnDateTime.Replace("muoi hai", "12");
                vnDateTime = vnDateTime.Replace("mot", "1");
                vnDateTime = vnDateTime.Replace("hai", "2");
                vnDateTime = vnDateTime.Replace("bay", "7");
                vnDateTime = vnDateTime.Replace("ba", "3");
                vnDateTime = vnDateTime.Replace("tu", "4");
                vnDateTime = vnDateTime.Replace("nam", "5");
                vnDateTime = vnDateTime.Replace("sau", "6");
                
                vnDateTime = vnDateTime.Replace("tam", "8");
                vnDateTime = vnDateTime.Replace("chin", "9");
                
                vnDateTime = vnDateTime.Replace("muoi", "10");

                var regex = new Regex("\\s+");
                vnDateTime = regex.Replace(vnDateTime, " ");
                var split = vnDateTime.Split(' ');
                if (split.Length >= 3)
                {
                    resultString = string.Format("{0}-{1}-{2}", split[2], split[1], split[0]);
                    if (split.Length > 3)
                        resultString = resultString + " " + split[3];
                }
            }

            return resultString;
        }
    }
}
